package cn.lg.soar.boot.fc.service.impl;

import cn.lg.soar.boot.fc.constants.LesseemanageConstant;
import cn.lg.soar.boot.fc.entity.vo.FcTreeVo;
import cn.lg.soar.boot.fc.tool.LesseemanageValidation;
import cn.lg.soar.common.data.msg.I18n;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.ParameterUtil;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.boot.fc.mapper.LesseemanageMapper;
import cn.lg.soar.boot.fc.entity.Lesseemanage;
import cn.lg.soar.boot.fc.service.LesseemanageService;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 承租人管理表
 * @author BoBo
 * @since 2025-02-05
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class LesseemanageServiceImpl extends BaseServiceImpl<LesseemanageMapper, Lesseemanage> implements LesseemanageService {


    @Override
    public HttpResult<Serializable> addLesseeManage(Lesseemanage lesseemanage) {
        //判断承租人类型
        if (lesseemanage.getLesseetype()==null){
            return HttpResult.fail("B0000", I18n.build("承租人类型不能为空！"));
        }
        //当承租人类型为个人时，证件号不能为空
        if (lesseemanage.getLesseetype()== LesseemanageConstant.LESSEETYPE_GR) {
            //证件号不能为空
            if (lesseemanage.getIdcard() == null || lesseemanage.getIdcard().isEmpty()) {
                return HttpResult.fail("B0000", I18n.build("证件号不能为空！"));
            }
            //先判断证件类型是否为空
            if (lesseemanage.getIdtype()==null) {
                return HttpResult.fail("B0000", I18n.build("证件类型不能为空！"));
            }
            //证件类型为居民身份证时
            if (lesseemanage.getIdtype()==LesseemanageConstant.IDTYPE_RESIDENT) {
                if (!LesseemanageValidation.isValidIDcard(lesseemanage.getIdcard())) {
                    return HttpResult.fail("B0000", I18n.build("证件号格式有误！"));
                }
            }
            //证件类型为护照时
            if (lesseemanage.getIdtype()==LesseemanageConstant.IDTYPE_PASSPORT) {
                if (!LesseemanageValidation.isValidpassport(lesseemanage.getIdcard())) {
                    return HttpResult.fail("B0000", I18n.build("证件号格式有误！"));
                }
            }
            //证件类型为港澳居民来往内地通行证
            if (lesseemanage.getIdtype()==LesseemanageConstant.IDTYPE_HG_AM) {
                if (!LesseemanageValidation.isValidGajmtxz(lesseemanage.getIdcard())) {
                    return HttpResult.fail("B0000", I18n.build("证件号格式有误！"));
                }
            }
            //证件类型为台湾居民来往大陆通行证
            if (lesseemanage.getIdtype()==LesseemanageConstant.IDTYPE_TAIWAN) {
                if (!LesseemanageValidation.isValidTwtxz(lesseemanage.getIdcard())) {
                    return HttpResult.fail("B0000", I18n.build("证件号格式有误！"));
                }
            }
        }

        //当承租人类型为公司时，营业执照号码不能为空
        if (lesseemanage.getLesseetype()==LesseemanageConstant.LESSEETYPE_GS) {
            if (lesseemanage.getLicensenumber()==null || lesseemanage.getLicensenumber().isEmpty()) {
                return HttpResult.fail("B0000", I18n.build("营业执照号码不能为空！"));
            }
        }
        //手机号校验，
        if (lesseemanage.getPhonenumber()!=null && !lesseemanage.getPhonenumber().isEmpty()) {
            if (!LesseemanageValidation.isValidPhone(lesseemanage.getPhonenumber())){
                return HttpResult.fail("B0000", I18n.build("联系方式格式有误！"));
            }
        }
        //法人身份证校验，调用身份证号正则校验
        if (lesseemanage.getLegalidcard()!=null && !lesseemanage.getLegalidcard().isEmpty()) {
            if (!LesseemanageValidation.isValidIDcard(lesseemanage.getLegalidcard())){
                return HttpResult.fail("B0000", I18n.build("法人身份证号式格式有误！"));
            }
        }

        ParameterUtil.notNull(lesseemanage, "数据不能为空");
        AssertUtil.isTrue(this.save(lesseemanage), "创建失败");
        return HttpResult.success("创建成功");
    }

    @Override
    public HttpResult<Serializable> updateLesseeManage(Lesseemanage lesseemanage) {
        //判断承租人类型
        if (lesseemanage.getLesseetype()==null){
            return HttpResult.fail("B0000", I18n.build("承租人类型不能为空！"));
        }
        //当承租人类型为个人时，证件号不能为空
        if (lesseemanage.getLesseetype()== LesseemanageConstant.LESSEETYPE_GR) {
            //证件号不能为空
            if (lesseemanage.getIdcard() == null || lesseemanage.getIdcard().isEmpty()) {
                return HttpResult.fail("B0000", I18n.build("证件号不能为空！"));
            }
            //先判断证件类型是否为空
            if (lesseemanage.getIdtype()==null) {
                return HttpResult.fail("B0000", I18n.build("证件类型不能为空！"));
            }
            //证件类型为居民身份证时
            if (lesseemanage.getIdtype()==LesseemanageConstant.IDTYPE_RESIDENT) {
                if (!LesseemanageValidation.isValidIDcard(lesseemanage.getIdcard())) {
                    return HttpResult.fail("B0000", I18n.build("证件号格式有误！"));
                }
            }
            //证件类型为护照时
            if (lesseemanage.getIdtype()==LesseemanageConstant.IDTYPE_PASSPORT) {
                if (!LesseemanageValidation.isValidpassport(lesseemanage.getIdcard())) {
                    return HttpResult.fail("B0000", I18n.build("证件号格式有误！"));
                }
            }
            //证件类型为港澳居民来往内地通行证
            if (lesseemanage.getIdtype()==LesseemanageConstant.IDTYPE_HG_AM) {
                if (!LesseemanageValidation.isValidGajmtxz(lesseemanage.getIdcard())) {
                    return HttpResult.fail("B0000", I18n.build("证件号格式有误！"));
                }
            }
            //证件类型为台湾居民来往大陆通行证
            if (lesseemanage.getIdtype()==LesseemanageConstant.IDTYPE_TAIWAN) {
                if (!LesseemanageValidation.isValidTwtxz(lesseemanage.getIdcard())) {
                    return HttpResult.fail("B0000", I18n.build("证件号格式有误！"));
                }
            }
        }

        //当承租人类型为公司时，营业执照号码不能为空
        if (lesseemanage.getLesseetype()==LesseemanageConstant.LESSEETYPE_GS) {
            if (lesseemanage.getLicensenumber()==null || lesseemanage.getLicensenumber().isEmpty()) {
                return HttpResult.fail("B0000", I18n.build("营业执照号码不能为空！"));
            }
        }
        //手机号校验，
        if (lesseemanage.getPhonenumber()!=null && !lesseemanage.getPhonenumber().isEmpty()) {
            if (!LesseemanageValidation.isValidPhone(lesseemanage.getPhonenumber())){
                return HttpResult.fail("B0000", I18n.build("联系方式格式有误！"));
            }
        }
        //法人身份证校验，调用身份证号正则校验
        if (lesseemanage.getLegalidcard()!=null && !lesseemanage.getLegalidcard().isEmpty()) {
            if (!LesseemanageValidation.isValidIDcard(lesseemanage.getLegalidcard())){
                return HttpResult.fail("B0000", I18n.build("法人身份证号式格式有误！"));
            }
        }

        ParameterUtil.notNull(lesseemanage, "数据不能为空");
        AssertUtil.isTrue(super.updateById(lesseemanage), "更新失败");
        return HttpResult.success("更新成功");
    }

    @Override
    public PageResult<FcTreeVo> queryResultVo(PageQuery pageQuery, QueryParams params) {
        PageResult<FcTreeVo> pageResultVo = new PageResult<>();
        SqueryWapper<Lesseemanage> squeryWapper = new SqueryWapper<>(Lesseemanage.class);

        PageResult<Lesseemanage> pageResult = squeryWapper.query(this, pageQuery, params);

        // 对象拷贝
        BeanUtils.copyProperties(pageQuery, pageResultVo);
        // 返回vo
        List<FcTreeVo> listVo = new ArrayList<>();

        // 时机
        pageResult.getList().forEach(item-> {

            FcTreeVo fcTreeVo = new FcTreeVo();
            // 校区父节点
            fcTreeVo.setParentId(("0"));
            fcTreeVo.setId(String.valueOf(item.getId()));
            fcTreeVo.setName(item.getTenantryname());
            listVo.add(fcTreeVo);
        });

        pageResultVo.setList(listVo);
        pageResultVo.setTotal(Long.valueOf(listVo.size()));
        return pageResultVo;
    }

    @Override
    public Lesseemanage getByName(String name) {

        if (StringUtils.isBlank(name)) {
            return new Lesseemanage();
        }
        return this.getFirst(Wrappers.<Lesseemanage>lambdaQuery().eq(Lesseemanage::getTenantryname, name));
    }

    @Override
    public Lesseemanage getOneById(Long id) {

        System.out.println("id ========================= " + id);
        if (Objects.isNull(id)) {
            return new Lesseemanage();
        }
        return this.getFirst(Wrappers.<Lesseemanage>lambdaQuery().eq(Lesseemanage::getId, id));
    }

    @Override
    public HttpResult<Serializable> updateSort(List<Map<String, BigInteger>> sortedData) {

        if (null == sortedData || 0 >= sortedData.size()) {
            return HttpResult.fail("4000", "排序数据不可为空", "error");
        }
        sortedData.forEach(item->{
            if (!item.containsKey("id") || !item.containsKey("orders")) {
                return;
            }
            Long id = item.get("id").longValue();;//基类型转换
            int orders = item.get("orders").intValue();//类型转换
            Lesseemanage lesseemanage = new Lesseemanage();
            lesseemanage.setId(id);
            lesseemanage.setOrders(orders);
            super.updateById(lesseemanage);
        });

        return  HttpResult.success("排序成功");
    }
}
